package com.hcryan.djm.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hcryan.djm.exception.ErrorMessage;
import com.hcryan.djm.exception.TravelException;
import com.hcryan.djm.pojo.entity.Itinerary;
import com.hcryan.djm.result.WebResult;
import com.hcryan.djm.service.ItineraryService;
import com.hcryan.djm.utils.MinioUtil;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author ryanwdj
 * @since 2025/5/3
 */
@RestController
@RequestMapping("/itinerary")
@Slf4j
@Api("行程")
public class ItineraryController {

    @Autowired
    private ItineraryService itineraryService;
    @Autowired
    private MinioUtil minioUtil;

    /**
     * 根据名称索引到具体推荐行程
     *
     * @param name
     */
    @GetMapping("/page")
    public WebResult<Page<Itinerary>> getItineraryPage(@RequestParam String name,
                                                       @RequestParam(defaultValue = "1") Integer pageNum,
                                                       @RequestParam(defaultValue = "10") Integer pageSize) {

        Page<Itinerary> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Itinerary> query = new LambdaQueryWrapper<>();
        query.eq(Itinerary::getName, name);
        Page<Itinerary> pageInfo = itineraryService.page(page, query);
        return WebResult.success(pageInfo);
    }

    /**
     * 发布行程，编辑简要展示页面
     * todo: 还有未完成内容
     *
     * @return
     */
    @PostMapping("/publish")
    public WebResult<Void> publishItinerary(@RequestParam("picture") MultipartFile picture) {
        try {
            String filePath = minioUtil.uploadFile(picture);
            String fileUrl = minioUtil.getFileUrl(filePath);
            return WebResult.success(fileUrl);
        } catch (Exception e) {
            log.error("文件上传失败", e);
            throw new TravelException(ErrorMessage.EXCEPTION, "文件上传失败");
        }
    }
}
